package TUILayer;
import java.util.Scanner;
import ControlLayer.*;


/**
 * Write a description of class LoanUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanUI
{
    // instance variables - replace the example below with your own
    private LoanCtr loanCtr;

    /* Imports the loan control */
    public LoanUI(LoanCtr loanCtr)
    {
        this.loanCtr = loanCtr;
    }

    /* Starts the program */
    public void start()
    {
        loanMenu();
    }
     /* The menu where you can chose */
    public void loanMenu()
    {
        boolean exit = false;
        while(!exit) {
            int choice = writeLoanMenu();
            if(choice == 1) {
                createLoan();
            }
            else {
                if(choice == 2) {
                    findLoan();
                }
                else {
                    if(choice == 3) {
                        deleteLoan();
                    }
                    else {
                        if(choice == 4) {
                            listAllLoans();
                        }
                        else {
                    exit = true;
                    }
                }
            }            
        }
    }
    }
     
     /* All the menus you can chose from */
    public int writeLoanMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Loan Menu ***");
        System.out.println(" (1) Create loan");
        System.out.println(" (2) Find loan");
        System.out.println(" (3) Delete loan");
        System.out.println(" (4) List all loans");
        System.out.println(" (5) Exit");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }
    
    /* Reads the input for the person ID */
     
    private long inputId()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the person id: ");
        long id = keyboard.nextInt();
        return id;
    }
     /* Reads the input for the copy id */
    private long inputCopyId()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the copy id");
        long id = keyboard.nextInt();
        return id;
    }
     /* Reades the input for the dvd ID */
    private long inputDVDId()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the DVD Id: ");
        int id = keyboard.nextInt();
        return id;
    }
    
     /* Reads the input for the date */
    private int inputDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the date: ");
        int date = keyboard.nextInt();
        return date;
    }
     /* Reads the input for the period a dvd is loan out  */
    private String inputPeriod()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the period: ");
        String period = keyboard.nextLine();
        return period;
    }
    
     /* Finds a loan  */
    private void findLoan()
    {
        long id = inputId();
        loanCtr.getLoan(id);
        Scanner keyboard = new Scanner(System.in); 
        keyboard.nextLine();
    }   
    
     /* Creates a loan */
    private void createLoan()
    {
        long borrowerId = inputId();
        if (loanCtr.isPerson(borrowerId))
        {
            
            long id = inputId();
            int borrowDate = inputDate();
            String period = inputPeriod();
            long copyId = inputCopyId();
            long dvdId = inputDVDId();            
            loanCtr.createLoan(loanCtr.getCopy(dvdId, copyId), id, borrowerId,borrowDate, period);
        }
        else
        {
            System.out.println("No such person");
        }
        waitForInput();
    }
    
     /* Deletes a loan */
    private void deleteLoan()
    {
        long id = inputId();
        loanCtr.deleteLoan(id);
    }
    
     /* Lists all loans */
    private void listAllLoans()
    {
        loanCtr.listAllLoans();
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
     /* Waits for an input to continue */
    private void waitForInput()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\nPress Enter to continue\n");
        keyboard.nextLine();
    }
}
